var latitude,longitude;
$(document).ready(function(e){
	mui.plusReady(function() {
		// userId = plus.navigator.getCookie( "id");
		function getPosition(){
			plus.geolocation.getCurrentPosition(function(p){
				latitude = p.coords.latitude;
				longitude = p.coords.longitude;
			}, function(error) {
			},{geocode:true},5000);
		}
		getPosition();
		// 按距离排序
		$("#select-near").on("click",function(){
			getPosition();
			$.ajax({
				url:backend_url + "/is/coll.action",
				data:{
					"latitude":latitude,//纬度
					"longitude":longitude//经度
				},
				type:"POST",
				dataType:"json",
				success:function(data){
					$("#product_list").empty();
					var page_list = data.b;
					// console.log(page_list);
					if(page_list.length < 1){
						$("#content_area").addClass("none");
					}else{
						$("#content_area").removeClass("none");
						for(var i = 0 ; i< page_list.length;i++){
							var issueTypeid = page_list[i].issueTypeid;
							var issueType = judge_issueType(issueTypeid);
							var imgLocation = page_list[i].imageLocation;
							var images = "";
							if(imgLocation != null && imgLocation.length != 0){
								images = imgLocation.split(";");
							}
							var li = `<li class="product_li" id=`+page_list[i].id+`>
										<a href="javascript:;">`;
							if(images.length>1){
								images.pop();
								li +=  `<img class="mui-pull-left" src=`+img_url+images[0]+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
							}else {
								// 单张图
								li += `<img class="mui-pull-left" src=`+img_url+images+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
							}
							var distance = Math.round(page_list[i].distance);
							li +=  `<div>
										<p class="title">`+page_list[i].issueTitle+`</p>
										<p class="issueType"><small>[</small><span>`+issueType+`</span><small>]</small><span class="mui-pull-right">`+distance+`m</span></p>
									</div>
								</a>
							</li>`;
							// console.log(li);
							$("#product_list").append(li);
						}
					}
				},
				error:function(er){
					// console.log(er);
					alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
					//window.location.reload();
				}
			});
		});
	});
	Product_show();
	
});
$(".mui-icon-more").click(function (){
	mui.toast('暂时没有更多了哦！');
});
// 下拉刷新
mui.init({
	pullRefresh : {
		container:"#content_area",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
		down : {
		  style:'circle',//必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式		 
		  callback :Product_show //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
		},
		up: {
			contentrefresh: '正在加载...',
			contentnomore: '没有更多数据了',
			callback :Product_show
		}
	}
});
// 获取所有的商品列表
function Product_show(){
	setTimeout(function() {
		$.ajax({
			url:backend_url + "/is/cl.action",
			type:"POST",
			dataType:"json",
			success:function(data){
				$("#product_list").empty();
				var page_list = data.b;
				// console.log(page_list);
				if(page_list.length < 1){
					$("#content_area").addClass("none");
				}else{
					$("#content_area").removeClass("none");
					for(var i = 0 ; i< page_list.length;i++){
						var issueTypeid = page_list[i].issueTypeid;
						var issueType = judge_issueType(issueTypeid);
						var imgLocation = page_list[i].imageLocation;
						var images = "";
						if(imgLocation != null && imgLocation.length != 0){
							images = imgLocation.split(";");
						}
							// 0: "/WebGMap/eg13hvspl1s6headImage/file_1556895226000.jpg"
							// 1: "/WebGMap/6p087rcag4lvheadImage/file_1556895221000.jpg"
							// 2: ""
						var li = `<li class="product_li" id=`+page_list[i].id+`>
									<a href="javascript:;">`;
						if(images.length>1){
							images.pop();
							li +=  `<img class="mui-pull-left" src=`+img_url+images[0]+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
						}else {
							// 单张图
							li += `<img class="mui-pull-left" src=`+img_url+images+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
						}
						li +=  `<div>
									<p class="title">`+page_list[i].issueTitle+`</p>
									<p class="issueType"><small>[</small><span>`+issueType+`</span><small>]</small></p>
								</div>
							</a>
						</li>`;
						// console.log(li);
						$("#product_list").append(li);
					}
				}
			},
			error:function(er){
				// console.log(er);
				alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
				//window.location.reload();
			}
		});
		mui('#content_area').pullRefresh().endPulldownToRefresh();
		mui('#content_area').pullRefresh().endPullupToRefresh();
	}, 10000);
}
// 通过issueTypeid判断issueType
function judge_issueType(issueTypeid){
	var issueType;
	if(issueTypeid == 1){
		issueType = "金属制品";
	}else if(issueTypeid == 2){
		issueType = "纸制品";
	}else if(issueTypeid == 3){
		issueType = "电子产品";
	}else if(issueTypeid == 4){
		issueType = "塑胶制品";
	}else{
		issueType = "木制品";
	}
	return issueType;
}

// 按时间排序
$("#select-new").on("click",function(){
	$.ajax({
		url:backend_url + "/is/gdsl.action",
		type:"POST",
		dataType:"json",
		success:function(data){
			$("#product_list").empty();
			var page_list = data.b;
			// console.log(page_list);
			if(page_list.length < 1){
				$("#content_area").addClass("none");
			}else{
				$("#content_area").removeClass("none");
				for(var i = 0 ; i< page_list.length;i++){
					var issueTypeid = page_list[i].issueTypeid;
					var issueType = judge_issueType(issueTypeid);
					var imgLocation = page_list[i].imageLocation;
					var images = "";
					if(imgLocation != null && imgLocation.length != 0){
						images = imgLocation.split(";");
					}
					var li = `<li class="product_li" id=`+page_list[i].id+`>
								<a href="javascript:;">`;
					if(images.length>1){
						images.pop();
						li +=  `<img class="mui-pull-left" src=`+img_url+images[0]+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
					}else {
						// 单张图
						li += `<img class="mui-pull-left" src=`+img_url+images+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
					}
					li +=  `<div>
								<p class="title">`+page_list[i].issueTitle+`</p>
								<p class="issueType">[<span>`+issueType+`</span>]<span class="mui-pull-right">`+page_list[i].dealTime+`</span></p>
							</div>
						</a>
					</li>`;
					// console.log(li);
					$("#product_list").append(li);
				}
			}
		},
		error:function(er){
			// console.log(er);
			alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
});
// 按分类排序
function SelectBytype(tid){
	$.ajax({
		url:backend_url + "/is/ctl.action",
		data:{
			"issueTypeid":tid
		},
		type:"POST",
		dataType:"json",
		success:function(data){
			$("#product_list").empty();
			var page_list = data.b;
			// console.log(page_list);
			if(page_list.length < 1){
				$("#content_area").addClass("none");
			}else{
				$("#content_area").removeClass("none");
				for(var i = 0 ; i< page_list.length;i++){
					var issueTypeid = page_list[i].issueTypeid;
					var issueType = judge_issueType(issueTypeid);
					var imgLocation = page_list[i].imageLocation;
					var images = "";
					if(imgLocation != null && imgLocation.length != 0){
						images = imgLocation.split(";");
					}
					var li = `<li class="product_li" id=`+page_list[i].id+`>
								<a href="javascript:;">`;
					if(images.length>1){
						images.pop();
						li +=  `<img class="mui-pull-left" src=`+img_url+images[0]+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
					}else {
						// 单张图
						li += `<img class="mui-pull-left" src=`+img_url+images+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
					}
					li +=  `<div>
								<p class="title">`+page_list[i].issueTitle+`</p>
								<p class="issueType"><small>[</small><span>`+issueType+`</span><small>]</small></p>
							</div>
						</a>
					</li>`;
					// console.log(li);
					$("#product_list").append(li);
				}
			}
		},
		error:function(er){
			// console.log(er);
			alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
}
// 模糊搜索
$("#sosuo").on("click",function(){
	var issueTitle = $("#sosuoval").val();
	$.ajax({
		url:backend_url + "/is/sll.action",
		data:{
			"issueTitle":issueTitle
		},
		type:"POST",
		dataType:"json",
		success:function(data){
			$("#product_list").empty();
			var page_list = data.b;
			// console.log(page_list);
			if(page_list.length < 1){
				$("#content_area").addClass("none");
			}else{
				$("#content_area").removeClass("none");
				for(var i = 0 ; i< page_list.length;i++){
					var issueTypeid = page_list[i].issueTypeid;
					var issueType = judge_issueType(issueTypeid);
					var imgLocation = page_list[i].imageLocation;
					var images = "";
					if(imgLocation != null && imgLocation.length != 0){
						images = imgLocation.split(";");
					}
					var li = `<li class="product_li" id=`+page_list[i].id+`>
								<a href="javascript:;">`;
					if(images.length>1){
						images.pop();
						li +=  `<img class="mui-pull-left" src=`+img_url+images[0]+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
					}else {
						// 单张图
						li += `<img class="mui-pull-left" src=`+img_url+images+` onerror="this.onerror='';src='images/error_img.png' " alt="产品图片">`;
					}
					li +=  `<div>
								<p class="title">`+page_list[i].issueTitle+`</p>
								<p class="issueType"><small>[</small><span>`+issueType+`</span><small>]</small></p>
							</div>
						</a>
					</li>`;
					// console.log(li);
					$("#product_list").append(li);
				}
			}
		},
		error:function(er){
			// console.log(er);
			alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
});